﻿using SoundEngine3.AudioProcessing;
using SoundEngine3.DspPlugins;
using SoundEngine3.MIDI.Meta;
using System;
using System.Collections.Generic;
using ZquenceStudio3.Core;
using ZquenceStudio3.Core.DspPlugins;
using ZquenceStudio3.DspPlugins;

namespace MyDspSynthesizerPlugins
{
    /// <summary>
    /// This description class is needed to let Zquence Studio find the DSP.
    /// </summary>
    public class MySimpleOscillatorDescription : IDspPlugin
    {
        public DspPluginInfo GetDspPluginInfo()
        {
            // Return the dsp information about the plugin and what kind of dsp it is.
            return new DspPluginInfo("MySimpleOscillator", DspType.Synthesizer);
        }

        public BaseDspPluginInstance CreateDsp(ZquenceStudio3Context context, DspPluginInfo info)
        {
            // This function is called when Zquence Studio want to create the actually DSP instance so let us create
            // the MySimpleOscillator synthesizer.
            return new MySimpleOscillator(context, info);
        }
    }

    public class MySimpleOscillator : CoreDspSynthesizerPlugin
    {
        public MySimpleOscillator(ZquenceStudio3Context soundEngineContext, DspPluginInfo dspPlugin)
            : base(soundEngineContext, dspPlugin)
        {
        }

        public override string PluginInfo
        {
            get
            {
                return "My simple oscillator";
            }
        }

        public override Version Version
        {
            get
            {
                return new Version(1, 0, 0, 0);
            }
        }
        
        /// <summary>
        /// This will be called when a midi event is signaled to the dsp.
        /// </summary>
        /// <param name="midiMessages">Collection of midi messages.</param>
        protected override void OnProcessEvents(List<MidiMessage> midiMessages)
        {

        }
        
        /// <summary>
        /// This will be called every buffer time only if this dsp is a synthesizer.
        /// </summary>
        /// <returns></returns>
        public override void OnProcess(BaseAudioBuffer output)
        {
            foreach (var channel in output.Channels)
            {
                for (int i = 0; i < channel.Samples.Length; i++)
                {
                    channel.Samples[i] = Math.Sin(i);
                }
            }
        }
    }
}